package org.bookie.web.pages;

import static org.bookie.core.services.impl.Listener.Event.BET_REMOVED;
import static org.bookie.core.services.impl.Listener.Event.BET_SLIP_PLACED;

import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.bookie.core.services.impl.Listener;
import org.bookie.framework.CommonBasePage;
import org.bookie.framework.components.IBetSlipComponent;
import org.bookie.framework.services.ChartGenerator.Chart;
import org.bookie.model.Event;
import org.bookie.model.Outcome;
import org.bookie.model.services.EventDAO;
import org.bookie.model.services.OutcomeDAO;

public abstract class EventDetails extends CommonBasePage
	implements IExternalPage, PageBeginRenderListener {

	public void activateExternalPage(Object[] parameters, IRequestCycle cycle) {
		if (parameters.length != 1) {
			recordError("eventdetails.event.id.missing");
		}
		setEventId((Long) parameters[0]);
		Event event = getEventDAO().findById(getEventId());
		if(event==null){
			recordError("eventdetails.event.notfound");
		}
		setEvent(event);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void pageBeginRender(PageEvent event) {
		if(getEvent()==null){
			setEvent(getEventDAO().findById(getEventId()));
		}
	}
	
	public boolean isSelected(Outcome outcome) {
		if (outcome == null) return false;
		return getBetSlip().isSelected(outcome);
	}
	
	public void addOutcome(Long id) {
		Outcome outcome = getOutcomeDAO().findById(id);
		getBetSlip().addOutcome(outcome);
	}
	
	@Listener(BET_REMOVED)
	public void betRemoved() {
		updateComponents("content");
	}
	
	@Listener(BET_SLIP_PLACED)
	public IPage betSlipPlaced() {
		return getRequestCycle().getPage("Home");
	}
	
	public ILink getChartLink(Long id, int width, int height){
		return getChartEngineService().getLink(false,
				new Object[]{	Chart.BET_DISTRIBUTION.value(),
								id,
								new Integer(width), new Integer(height)});
	}
	
	@Component(id = "betSlip", type = "bookie:BetSlip", bindings={
			"loginListener=components.border.login.loginListener"
	})
	public abstract IBetSlipComponent getBetSlip();
	
	/**
	 * Injected service
	 * 
	 * @return
	 */
	@InjectObject("service:bookie.model.OutcomeDAO")
	public abstract OutcomeDAO getOutcomeDAO();
	/**
	 * Injected {@link EventDAO} service.
	 * 
	 * @return instance of EventDAO
	 */
	@InjectObject("service:bookie.model.EventDAO")
	public abstract EventDAO getEventDAO();
	
	public abstract Event getEvent();
	public abstract void setEvent(Event event);
	
	@Persist
	public abstract Long getEventId();
	public abstract void setEventId(Long value);
	
	@InjectObject("service:bookie.framework.ChartEngine")
	public abstract IEngineService getChartEngineService();


}
